package com.telra.app.server.cassandra;

import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.factory.HFactory;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Log4JLogger;

/**
 * Date: 1/5/12
 * Time: 10:01 PM
 */
public class CassandraService {

    private final static Log4JLogger log = (Log4JLogger) LogFactory.getFactory().getInstance(CassandraService.class);

    private String clusterName;
    private String host;
    private String port;
    private String keyspaceName;

    private Cluster cluster;
    private Keyspace keyspace;

    public CassandraService() {
        //empty
    }

    private void init()  {
        log.info("Connecting to cluster " + clusterName);
        this.cluster = HFactory.getOrCreateCluster(clusterName, host+":"+port);
        log.info("Connecting to keyspace " + keyspaceName);
        this.keyspace = HFactory.createKeyspace(keyspaceName, cluster);
        log.info("Cassandra initialized");
    }


    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getKeyspaceName() {
        return keyspaceName;
    }

    public void setKeyspaceName(String keyspaceName) {
        this.keyspaceName = keyspaceName;
    }

    public String getClusterName() {
        return clusterName;
    }

    public void setClusterName(String clusterName) {
        this.clusterName = clusterName;
    }

    
    
    public Keyspace getKeyspace() {
        if (keyspace == null) {
            init();
        }
        return keyspace;
    }
    
    public Cluster getCluster() {
        if (cluster == null) {
            init();
        }
        return cluster;
    }

}
